<?
// +----------------------------------------------------------------------
// | scrape.php - MyPT
// +----------------------------------------------------------------------
// | Copyleft (c) 2010 By Asxzy All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Asxzy <asxzy@163.com>
// +----------------------------------------------------------------------
define('THINK_PATH', './ThinkPHP');

require_once("backend/bittorrent.php");
require_once("backend/benc.php");

function err($msg){
	benc_resp(array("failure reason" => array(type => "string", value => $msg)));
	exit();
}
dbconn(false);

$r = "d" . benc_str("files") . "d";

$fields = "info_hash, times_completed, seeders, leechers";
$info_hash = urldecode($_GET["info_hash"]);

if (!isset($info_hash))
	$query = "SELECT $fields FROM torrents ORDER BY info_hash";
else{
	if (strlen($info_hash) != 20) err("Invalid Passkey");
	$info_hash = bin2hex($info_hash);
	$query = "SELECT $fields FROM torrents WHERE " . hash_where("info_hash", $info_hash);
}
$res = mysql_query($query);
while ($row = mysql_fetch_assoc($res)) {
	$r .= "20:" . $_GET["info_hash"] . "d" .
		benc_str("complete") . "i" . $row["seeders"] . "e" .
		benc_str("downloaded") . "i" . $row["times_completed"] . "e" .
		benc_str("incomplete") . "i" . $row["leechers"] . "e" .
		"e";
}

$r .= "ee";

//header("Content-Type: text/plain");
print($r);

?>
